package cn.cloudreal.cbms.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;

import cn.cloudreal.cbms.Dictionary.dao.impl.DictionaryDaoImpl;
import cn.cloudreal.cbms.Dictionary.service.DictionaryService;
import cn.cloudreal.cbms.vo.Dictionary;

/**
 * 数据字典上线文类 需要将OrganizaAction.action、RoleAction.action一起替换掉
 * 
 * @author gao_xuejiao
 * @data 2012-07-12
 */
public class DatadicContext {

	// map 集合
	private Map<String, List<Dictionary>> datadic = new HashMap<String, List<Dictionary>>();

	// context 对象
	private static DatadicContext datadicCtx;

	// 上下文对象
	private WebApplicationContext ctx;

	// 日志
	public static final Log log = LogFactory.getLog(DictionaryDaoImpl.class);

	// 构造函数
	private DatadicContext() {

	}

	/**
	 * 初始化数据字典
	 * 
	 * @return
	 */
	public static DatadicContext getInstance() {
		if (datadicCtx != null) {
			return datadicCtx;
		}

		datadicCtx = new DatadicContext();

		return datadicCtx;
	}

	/**
	 * 加载数据字典信息
	 */
	public void reload() {
		log.debug("load dictionary Object list!");
		// 获取数据字典service 对象
		DictionaryService dataDicService = (DictionaryService) ctx
				.getBean("dictionaryService");

		// 获取所有父类集合
		List<Dictionary> dataDics = dataDicService.getAllDictionarys();

		/**
		 * 遍历需找孩子节点
		 */
		for (Iterator<Dictionary> iterator = dataDics.iterator(); iterator
				.hasNext();) {
			Dictionary dictionary = (Dictionary) iterator.next();
			List<Dictionary> sonsDictionarys = dataDicService
					.findByParentId(dictionary.getId());
			if (sonsDictionarys.size() > 0) {
				datadic.put(dictionary.getClasscode(), sonsDictionarys);
			}

		}
	}

	/**
	 * 数据字典新建修改和删除的时候，刷新内存数据。
	 * 
	 * @param classCode
	 *            TODO:后台更新时调用该方法
	 */
	synchronized public void reloadByCode(String classCode) {
		log.debug("refresh dictionary Object list!");

		// 获取数据字典service 对象
		DictionaryService dataDicService = (DictionaryService) ctx
				.getBean("dictionaryService");

		int pid = dataDicService.findByClassCode(classCode, null).getParentId();

		Dictionary dictionary = dataDicService.findByID(pid);

		List<Dictionary> list = dataDicService.findByParentId(pid);

		datadic.put(dictionary.getClasscode(), list);

		log.debug("refresh dictionary Object list end!");
	}

//	/**
//	 * 根据数据字典编码取出所有孩子节点 TODO:BXL--->根据数据字典编码查找出所有孩子解决
//	 * 
//	 * @param type
//	 * @return
//	 */
//	public List<Dictionary> findDicDataList(String classCode, String parentId) {
//		log.debug("get dictionary Object list!");
//		// 获取数据字典service 对象
//		DictionaryService dataDicService = (DictionaryService) ctx
//				.getBean("dictionaryService");
//		List<Dictionary> results = dataDicService.findDicDataList(classCode, parentId);
//		return results;
//	}
	
	public List<Dictionary> getDicDataListByCode(String classCode) {
		log.debug("get dictionary Object list!");

		// 判断类型是否存在
		if (null == classCode || StringUtils.isEmpty(classCode)) {
			return null;
		}
		List<Dictionary> results = new ArrayList<Dictionary>();
		results = datadic.get(classCode);
		return results;
	}

	public Dictionary getDatadicObject(String subcode, String id) {
		// 获取数据字典service 对象
		DictionaryService dataDicService = (DictionaryService) ctx
				.getBean("dictionaryService");
		Dictionary dictionary = dataDicService.findByClassCode(subcode, id);
		return dictionary;

	}

//	public Dictionary getDatadicObjectByIdAndClassCode(String id, String classCode) {
//		// 获取数据字典service 对象
//		DictionaryService dataDicService = (DictionaryService) ctx
//				.getBean("dictionaryService");
//		Dictionary dictionary = dataDicService.findByIdAndClassCode(id, classCode);
//		return dictionary;
//
//	}

	public void setCtx(WebApplicationContext ctx) {
		this.ctx = ctx;
	}
}
